package com.pdool.h5red;

/**
 * @author 种鑫
 * @date 2023/12/28 17:14
 */

import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import java.io.IOException;

@Component
@WebFilter(filterName = "MyFilter", urlPatterns = "/*")

public class TimeFilter implements Filter {
    @Resource
    GameConfig gameConfig;
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        if (System.currentTimeMillis() >= gameConfig.getEndTime()){
            servletResponse.setContentType("text/plain");
            servletResponse.setCharacterEncoding("UTF-8");
            servletResponse.getWriter().write("活动已关闭");
            servletResponse.getWriter().flush();
            servletResponse.getWriter().close();
        }
        else {
            filterChain.doFilter(servletRequest,servletResponse);
        }
    }
}
